package com.admgt.resource.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.util.Observable;
import java.util.Observer;

import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JPopupMenu;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.tree.TreePath;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.springframework.test.context.ContextConfiguration;



/**
 * User interface for resource management
 * 
 * @author James
 */
public class ResourceFrame extends javax.swing.JFrame {
	private static final long serialVersionUID = 2376155285845383278L;
	private JPanel panelSouth;
	private JPanel panelEast;
	private JPanel panelWest;
	private JPanel panelNorth;

	/** resourceContext 客户端上下文, 这里是客户端控制器 */
	private ResourceContext resourceContext;// 默认值是null
	/** Ftp connection information */
	private JTextField hostname;
	private JTextField username;
	private JPasswordField password;
	private JTextField port;
	private FileTable fileTable;
	private String ftpServerFileName;
	private JTextField textFtpPath;
	private String localPath;
	private LocalTree jTree;
	private TreePath[] paths;
	
	private ProgressTableModel progressTableModel;
	private ProgressBarObservable progressBarObservable;
	private ProgressBarObserver progressBarObserver;


	public TreePath[] getPaths() {
		return paths;
	}

	public String getLocalPath() {
		return localPath;
	}

	public JTextField getTextFtpPath() {
		return textFtpPath;
	}

	public String getFtpServerFileName() {
		return ftpServerFileName;
	}

	public String getHostName() {
		String str = hostname.getText();
		return str;
	}

	public String getUserName() {
		String str = username.getText();
		return str;
	}

	public String getPassword() {
		char[] pwd = password.getPassword();
		return new String(pwd);
	}

	public int getPort() {
		String str = port.getText();
		return str == "" ? 0 : Integer.parseInt(str);
	}

	/**
	 * 为resourceContext赋值, 使resourceContext引用的值, 不再为null, 必须依赖的属性值注入方法 缩写: IOC
	 */
	public void setResourceContext(ResourceContext resourceContext) {
		this.resourceContext = resourceContext;
	}
	

	public ResourceContext getResourceContext() {
		return resourceContext;
	}

	public ResourceFrame() {
		init();
	}

	private void init() {
		setTitle("资源管理");
		setSize(800, 600);
		setContentPane(createContentPane());
		setMenu();// 设置窗口的菜单
		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		pack();
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		progressBarObservable = new ProgressBarObservable();
		progressBarObserver = new ProgressBarObserver();
		progressBarObservable.addObserver(progressBarObserver);
	}

	private Container createContentPane() {
		JPanel panelBoard = new JPanel(new BorderLayout());
		panelBoard.add(BorderLayout.NORTH, createNorthPane());
		panelBoard.add(BorderLayout.CENTER, createCenterPane());
		panelBoard.add(BorderLayout.SOUTH, createSouthPane());
		panelBoard.setBorder(BorderFactory.createLineBorder(Color.red));
		return panelBoard;
	}

	private Component createCenterPane() {
		JPanel panelCenter = new JPanel(new BorderLayout());
		JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
				createWestPane(), createEastPane());
		splitPane.setDividerLocation(250);
		splitPane.setOneTouchExpandable(true);
		panelCenter.add(splitPane, BorderLayout.CENTER);
		panelCenter.setBorder(BorderFactory.createLineBorder(Color.red));
		return panelCenter;
	}

	private void setMenu() {
		JMenuBar jMenuBar1 = new javax.swing.JMenuBar();
		JMenu jMenu1 = new javax.swing.JMenu();
		JMenuItem jMenuItem1 = new javax.swing.JMenuItem();
		JMenu jMenu2 = new javax.swing.JMenu();
		jMenu1.setText("File");
		jMenuItem1.setText("jMenuItem1");
		jMenu1.add(jMenuItem1);
		jMenuBar1.add(jMenu1);
		jMenu2.setText("Edit");
		jMenuBar1.add(jMenu2);
		setJMenuBar(jMenuBar1);
		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout
				.setHorizontalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
																panelNorth,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																Short.MAX_VALUE)
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addComponent(
																				panelWest,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				Short.MAX_VALUE)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addComponent(
																				panelEast,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				javax.swing.GroupLayout.PREFERRED_SIZE))
														.addComponent(
																panelSouth,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																Short.MAX_VALUE))
										.addContainerGap()));
		layout
				.setVerticalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addComponent(
												panelNorth,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
																panelEast,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																panelWest,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addGap(24, 24, 24)
										.addComponent(
												panelSouth,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addGap(0, 0, Short.MAX_VALUE)));
	}

	private Component createNorthPane() {
		panelNorth = new JPanel();
		JLabel labelHostName = new javax.swing.JLabel();
		hostname = new javax.swing.JTextField();
		hostname.setText("127.0.0.1");
		JLabel labelUserName = new javax.swing.JLabel();
		username = new javax.swing.JTextField();
		username.setText("myusername");
		JLabel labelPassword = new javax.swing.JLabel();
		password = new javax.swing.JPasswordField();
		password.setText("12345678");
		JLabel labelPort = new javax.swing.JLabel();
		port = new javax.swing.JTextField();
		port.setText("21");
		JButton ftpConnect = new javax.swing.JButton();

		labelHostName.setText("FTP主机IP：");
		labelUserName.setText("用户名：");
		labelPassword.setText("密码：");
		labelPort.setText("端口：");
		ftpConnect.setText("连接");

		hostname.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				hostNameActionPerformed(evt);
			}
		});

		ftpConnect.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				ftpConnectActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout panelNorthLayout = new javax.swing.GroupLayout(
				panelNorth);
		panelNorth.setLayout(panelNorthLayout);
		panelNorthLayout
				.setHorizontalGroup(panelNorthLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								panelNorthLayout
										.createSequentialGroup()
										.addContainerGap()
										.addComponent(labelHostName)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(
												hostname,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												206,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
										.addComponent(labelUserName)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(
												username,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												122,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
										.addComponent(labelPassword)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(
												password,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												173,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(labelPort)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(
												port,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												33,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(ftpConnect).addGap(41,
												41, 41)));
		panelNorthLayout
				.setVerticalGroup(panelNorthLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								panelNorthLayout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												panelNorthLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																panelNorthLayout
																		.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.BASELINE)
																		.addComponent(
																				labelHostName)
																		.addComponent(
																				hostname,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addComponent(
																				labelUserName)
																		.addComponent(
																				username,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addComponent(
																				password,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addComponent(
																				labelPort)
																		.addComponent(
																				port,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addComponent(
																				ftpConnect))
														.addComponent(
																labelPassword))
										.addContainerGap(21, Short.MAX_VALUE)));
		panelNorth.setBorder(BorderFactory.createLineBorder(Color.blue));

		return panelNorth;
	}

	private Component createWestPane() {
		panelWest = new JPanel(new BorderLayout());
		JLabel labelLocalPath = new javax.swing.JLabel();
		JScrollPane jScrollPane = new javax.swing.JScrollPane();
		jTree = new LocalTree();

		final JPopupMenu menu = new JPopupMenu("PopupMenu");// 右键
		JMenuItem upload = new JMenuItem("Upload...");
		JMenuItem uploadAs = new JMenuItem("Upload As...");
		upload.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				uploadActionPerformed(e);
			}
		});
		uploadAs.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(panelWest, e.getActionCommand());

			}
		});
		menu.add(upload);
		menu.add(uploadAs);
		menu.setSize(50, 100);
		jTree.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				paths = null;
				if (SwingUtilities.isRightMouseButton(e)) {
					int n = jTree.getSelectionCount();
					if (n == 0) {
						int selRow = jTree
								.getRowForLocation(e.getX(), e.getY());
						// TreePath selPath =
						// jTree.getPathForLocation(e.getX(),e.getY());
						if (selRow != -1) {
							jTree.setSelectionRow(selRow);
						}
						paths = jTree.getSelectionPaths();
						for (TreePath path : paths) {
							System.out.print(path.toString() + "\t");
						}
						System.out.println();
					} else {
						paths = jTree.getSelectionPaths();
						for (TreePath path : paths) {
							System.out.print(path.toString() + "\t");
						}
						System.out.println();
					}
					menu.show(e.getComponent(), e.getX(), e.getY());
				} else if (SwingUtilities.isLeftMouseButton(e)) {
					paths = jTree.getSelectionPaths();
				}
			}

		});
		
		jTree.addTreeSelectionListener(new TreeSelectionListener(){
			@Override
			public void valueChanged(TreeSelectionEvent e) {
				paths = jTree.getSelectionPaths();
				System.out.println("valueChanged");
				for (TreePath path : paths) {
					System.out.print(path.toString() + "\t");
				}
				System.out.println("--------------");
			}
		});

		JButton buttonUpload = new javax.swing.JButton();
		JButton btCancelSelected = new javax.swing.JButton();

		labelLocalPath.setText("本地目录：");
		panelWest.setBorder(BorderFactory.createTitledBorder("本地目录："));
		jScrollPane.setViewportView(jTree);
		buttonUpload.setText("批量上传");
		btCancelSelected.setText("取消选择");

		javax.swing.GroupLayout panelWestLayout = new javax.swing.GroupLayout(
				panelWest);
		panelWest.setLayout(panelWestLayout);
		panelWestLayout
				.setHorizontalGroup(panelWestLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								panelWestLayout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												panelWestLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
																jScrollPane)
														.addGroup(
																panelWestLayout
																		.createSequentialGroup()
																		.addGroup(
																				panelWestLayout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING)
																						.addComponent(
																								labelLocalPath)
																						.addGroup(
																								panelWestLayout
																										.createSequentialGroup()
																										.addComponent(
																												buttonUpload)
																										.addPreferredGap(
																												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																										.addComponent(
																												btCancelSelected)))
																		.addGap(
																				0,
																				0,
																				Short.MAX_VALUE)))
										.addContainerGap()));
		panelWestLayout
				.setVerticalGroup(panelWestLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								panelWestLayout
										.createSequentialGroup()
										.addContainerGap()
										.addComponent(labelLocalPath)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(
												jScrollPane,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												335,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												panelWestLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																buttonUpload)
														.addComponent(
																btCancelSelected))
										.addContainerGap(
												javax.swing.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)));
		panelWest.setBorder(BorderFactory.createLineBorder(Color.green));

		return panelWest;
	}

	private Component createEastPane() {
		panelEast = new JPanel(new BorderLayout());
		JLabel labelFtpPath = new javax.swing.JLabel();
		textFtpPath = new javax.swing.JTextField();
		textFtpPath.setText("/");
		JLabel labelFileType = new javax.swing.JLabel();
		JComboBox comboFileType = new javax.swing.JComboBox();
		JButton buttonFilter = new javax.swing.JButton();
		JScrollPane jScrollPane = new javax.swing.JScrollPane();

		labelFtpPath.setText("FTP目录：");
		labelFileType.setText("文件类型：");
		comboFileType.setModel(new DefaultComboBoxModel(new String[] { "txt",
				"wmv", "doc", "xsl", "ppt", "mp3", "mp4", "rmvb" }));
		buttonFilter.setText("过滤");

		String[] tableTitle = getTableTitle();
		Object[][] tableData = null;
		if (resourceContext != null && resourceContext.getFtpClient().isConnected()) {
			tableData = resourceContext.getTableData();
		}
		fileTable = new FileTable(tableData, tableTitle);

		final JPopupMenu menu = new JPopupMenu("PopupMenu");// 右键
		JMenuItem download = new JMenuItem("Download...");
		JMenuItem downloadAs = new JMenuItem("Download As...");
		download.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				localPath = null;
				JFileChooser fChooser = new JFileChooser();
				fChooser.setDialogTitle("请选择要下载的路径...");
				fChooser.setApproveButtonText("确 定");
				fChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				if (JFileChooser.APPROVE_OPTION == fChooser
						.showOpenDialog(ResourceFrame.this)) {
					localPath = fChooser.getSelectedFile().getPath() + "/"
							+ ftpServerFileName;
				}
				System.out.println("download target=" + localPath);
				
				Thread downloadThread = new downloadThread();
				downloadThread.start();
				
				progressTableModel.addProcessBar();
				Thread t= new ProgressBarThread();
				t.start();
			}
		});

		downloadAs.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				localPath = null;
				JFileChooser fChooser = new JFileChooser();
				fChooser.setDialogTitle("请选择要下载的路径并输入文件名...");
				fChooser.setApproveButtonText("确 定");
				fChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
				if (JFileChooser.APPROVE_OPTION == fChooser
						.showOpenDialog(ResourceFrame.this)) {
					localPath = fChooser.getSelectedFile().getPath();
				}
				System.out.println("download target=" + localPath);
				resourceContext.download();
			}
		});

		menu.add(download);
		menu.add(downloadAs);
		menu.setSize(50, 100);
		fileTable.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (SwingUtilities.isRightMouseButton(e)) {
					final int selectedRow = fileTable.rowAtPoint(e.getPoint());
					System.out.println("selectedRow:" + selectedRow);
					if (selectedRow != -1) {
						fileTable.setRowSelectionInterval(selectedRow,
								selectedRow); // 高亮选择指定的行
					}
					ftpServerFileName = fileTable.getValueAt(
							fileTable.getSelectedRow(), 0).toString();
					System.out
							.println("RemoteFileName is:" + ftpServerFileName);

					menu.show(e.getComponent(), e.getX(), e.getY());
				} else if (SwingUtilities.isLeftMouseButton(e)) {
					ftpServerFileName = fileTable.getValueAt(
							fileTable.getSelectedRow(), 0).toString();
					System.out
							.println("RemoteFileName is:" + ftpServerFileName);
				}
			}

			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					// menu.show(e.getComponent(), e.getX(), e.getY());
				}
			}
		});

		jScrollPane.setViewportView(fileTable);

		textFtpPath.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				textFtpPathActionPerformed(evt);
			}
		});

		buttonFilter.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				buttonFilterActionPerformed(evt);
			}
		});
		javax.swing.GroupLayout panelEastLayout = new javax.swing.GroupLayout(
				panelEast);
		panelEast.setLayout(panelEastLayout);
		panelEastLayout
				.setHorizontalGroup(panelEastLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								panelEastLayout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												panelEastLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
																jScrollPane)
														.addGroup(
																panelEastLayout
																		.createSequentialGroup()
																		.addComponent(
																				labelFtpPath)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addComponent(
																				textFtpPath,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				225,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED,
																				42,
																				Short.MAX_VALUE)
																		.addComponent(
																				labelFileType)
																		.addGap(
																				32,
																				32,
																				32)
																		.addComponent(
																				comboFileType,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				80,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addGap(
																				41,
																				41,
																				41)
																		.addComponent(
																				buttonFilter)
																		.addGap(
																				31,
																				31,
																				31)))
										.addContainerGap()));
		panelEastLayout
				.setVerticalGroup(panelEastLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								panelEastLayout
										.createSequentialGroup()
										.addContainerGap(
												javax.swing.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)
										.addGroup(
												panelEastLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																labelFtpPath)
														.addComponent(
																textFtpPath,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																labelFileType)
														.addComponent(
																comboFileType,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																buttonFilter))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
										.addComponent(
												jScrollPane,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												348,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addContainerGap()));
		panelEast.setBorder(BorderFactory.createLineBorder(Color.cyan));
		return panelEast;
	}

	private String[] getTableTitle() {
		String[] tableTitle = new String[] { "文件名", "文件大小", "文件类型", "修改时间",
				"属性", "序号" };
		return tableTitle;
	}

	private Object[][] getTableData(FTPClient ftpClient) {
		Object tableData[][] = null;
		try {
			if (ftpClient.isConnected()) {
				FTPFile[] files = ftpClient.listFiles();
				int n = files.length;
				tableData = new Object[n][6];
				for (int i = 0; i < n; i++) {
					System.out.println(files[i].getRawListing());
					tableData[i][0] = files[i].getName();
					tableData[i][1] = files[i].getSize();
					tableData[i][2] = files[i].isDirectory() ? "文件夹" : "文件";
					tableData[i][3] = files[i].getTimestamp().getTime();
					tableData[i][4] = files[i].getRawListing().substring(0, 9);
					tableData[i][5] = i + 1;
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(this, e.getMessage());
		}

		return tableData;
	}

	private Component createSouthPane() {
		panelSouth = new JPanel(new BorderLayout());
		JLabel labelLoadMsg = new javax.swing.JLabel();
		JScrollPane jScrollPane = new javax.swing.JScrollPane();
		labelLoadMsg.setText("上下载信息：");
		progressTableModel = new ProgressTableModel();
		JTable pTable = new JTable(progressTableModel);
		// 设置"Status"列由定制的ProgressBarRenderer渲染
		TableColumn progressColumn = pTable.getColumn("进度");
		progressColumn.setCellRenderer(new DefaultTableCellRenderer() {
			private static final long serialVersionUID = 1L;

			private final JProgressBar bar;

			{
				setOpaque(true);
				bar = new JProgressBar();
				bar.setStringPainted(true);
				bar.setMaximum(100);
				bar.setMinimum(0);
				bar.setBorderPainted(true);
				bar.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
			}

			public Component getTableCellRendererComponent(JTable table,
					Object value, boolean isSelected, boolean hasFocus,
					int row, int column) {
				Integer n = (Integer) value;
				bar.setValue(n);
				return bar;
			}
		});
		jScrollPane.setViewportView(pTable);

		/*
		 * JTable jTable = new javax.swing.JTable(); jTable.setModel(new
		 * javax.swing.table.DefaultTableModel(new Object[][] { { null, null,
		 * null, null, null, null, null, null }, { null, null, null, null, null,
		 * null, null, null }, { null, null, null, null, null, null, null, null
		 * }, { null, null, null, null, null, null, null, null }, { null, null,
		 * null, null, null, null, null, null }, { null, null, null, null, null,
		 * null, null, null }, { null, null, null, null, null, null, null, null
		 * }, { null, null, null, null, null, null, null, null }, { null, null,
		 * null, null, null, null, null, null }, { null, null, null, null, null,
		 * null, null, null } }, new String[] { "文件名", "文件大小", "主机名", "状态",
		 * "本地路径", "服务器路径", "进度", "完成度" }) { private static final long
		 * serialVersionUID = 6672611146770062238L;
		 * 
		 * @SuppressWarnings("unchecked") Class[] types = new Class[] {
		 * java.lang.String.class, java.lang.Long.class, java.lang.String.class,
		 * java.lang.String.class, java.lang.String.class,
		 * java.lang.String.class, java.lang.Object.class,
		 * java.lang.String.class };
		 * 
		 * @SuppressWarnings("unchecked") public Class getColumnClass(int
		 * columnIndex) { return types[columnIndex]; } });
		 * jScrollPane.setViewportView(jTable);
		 */

		javax.swing.GroupLayout panelSouthLayout = new javax.swing.GroupLayout(
				panelSouth);
		panelSouth.setLayout(panelSouthLayout);
		panelSouthLayout
				.setHorizontalGroup(panelSouthLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								panelSouthLayout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												panelSouthLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																panelSouthLayout
																		.createSequentialGroup()
																		.addComponent(
																				labelLoadMsg)
																		.addGap(
																				0,
																				0,
																				Short.MAX_VALUE))
														.addComponent(
																jScrollPane))
										.addContainerGap()));
		panelSouthLayout.setVerticalGroup(panelSouthLayout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				panelSouthLayout.createSequentialGroup().addContainerGap()
						.addComponent(labelLoadMsg).addGap(18, 18, 18)
						.addComponent(jScrollPane,
								javax.swing.GroupLayout.PREFERRED_SIZE, 111,
								javax.swing.GroupLayout.PREFERRED_SIZE)
						.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
								Short.MAX_VALUE)));
		panelSouth.setBorder(BorderFactory.createLineBorder(Color.lightGray));
		return panelSouth;
	}

	private void hostNameActionPerformed(ActionEvent e) {// GEN-FIRST:event_textHostNameActionPerformed
		// TODO add your handling code here:
	}// GEN-LAST:event_textHostNameActionPerformed

	private void ftpConnectActionPerformed(ActionEvent e) {
		if (resourceContext.getFtpClient() != null
				&& resourceContext.getFtpClient().isConnected()) {
			updateFileTable();
		} else {
			resourceContext.connectFtpServer();
			updateFileTable();
		}
	}

	private void uploadActionPerformed(ActionEvent e) {
		progressTableModel.addProcessBar();
		Thread t= new ProgressBarThread();
		t.start();
		resourceContext.upload();
		//fileTable.repaint();
	}

	private void updateFileTable() {
		fileTable.updateTableData(
				getTableData(resourceContext.getFtpClient()),
				getTableTitle());
		fileTable.repaint();
	}

	private void buttonFilterActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_buttonFilterActionPerformed
		// TODO add your handling code here:
	}// GEN-LAST:event_buttonFilterActionPerformed

	private void textFtpPathActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_textFtpPathActionPerformed
		// TODO add your handling code here:
	}// GEN-LAST:event_textFtpPathActionPerformed

	
	
	public class ProgressBarThread extends Thread {
		int progress =0;
		int rowIndex = progressTableModel.getRowCount()-1;
		String localPath = getLocalPath();
		String ftpServerFileName = getFtpServerFileName();
		
		public void run(){
			Object oId = progressTableModel.getValueAt(rowIndex, 0);
			System.out.println("oId= "+oId);
			int id = Integer.parseInt(oId.toString());
			System.out.println("id= "+id);
			while(progress<100){
				try{
					Thread.sleep(100);
					progress++ ;
					//progress = resourceContext.updateProgress(id,ftpServerFileName,localPath);
					//System.out.println("progress= "+progress);
					SwingUtilities.invokeLater(new Runnable(){
						public void run(){
							progressBarObservable.setShowValue(rowIndex+":"+progress);
						}
					});
				}catch(Exception e){
					e.printStackTrace();
				}
			}
			System.out.println("download percent= "+progress);
		}
	}
	public class downloadThread extends Thread {
		public void run(){
			SwingUtilities.invokeLater(new Runnable(){
				public void run(){
					resourceContext.download();
				}
			});
		}
	}

	
	public class ProgressBarObserver implements Observer{
		@Override
		public void update(Observable o, Object arg) {
				String showValue = (String)arg;
				//System.out.println("value = "+showValue.split(":")[1]+" "+showValue.split(":")[0]);
				progressTableModel.setValueAt(
						Integer.parseInt(showValue.split(":")[1]),
						Integer.parseInt(showValue.split(":")[0]),
						7);
		}
	}
	
	public class ProgressBarObservable extends Observable{
		private String showValue;
		public String getShowValue(){
			return showValue;
		}
		public void setShowValue(String showValue) {
			this.showValue = showValue;
			setChanged();
			notifyObservers(showValue);
		}
		
	}
	
	class ProgressTableModel extends DefaultTableModel{
		private static final long serialVersionUID = 1L;
		public Object[] rowData = {"0",".dd.","test","as","fd","rte","rt",0,"erter"};
		String[] tableColumns = new String[] {"ID", 
				"文件名", "文件大小", "主机名", "状态",
				"本地路径", "服务器路径","进度", "完成度" };
		public ProgressTableModel() {
			super();
			for(String s: tableColumns){
				addColumn(s);
			}
		}
		@Override
		public boolean isCellEditable(int row, int column) {
			return false;
		}
		

		public void addProcessBar(){
			rowData[1] = "File"+ (this.getRowCount()+1);
			//rowData = resourceContext.addRowData();
			addRow(rowData);
		}
		

	}

	/** 
	* 隐藏表格中的某一列 
	* @param table  表格 
	* @param index  要隐藏的列 的索引 
	*/ 
	private void hideColumn(JTable table,int index){ 
	TableColumn tc= table.getColumnModel().getColumn(index); 
	tc.setMaxWidth(0); 
	tc.setPreferredWidth(0); 
	tc.setMinWidth(0); 
	tc.setWidth(0); 

	table.getTableHeader().getColumnModel().getColumn(index).setMaxWidth(0); 
	table.getTableHeader().getColumnModel().getColumn(index).setMinWidth(0); 
	} 
	

}
